package com.example.nophase.custom;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.nophase.R;

public class TextFieldDialog extends Dialog {


    private Context context;
    private String title = "";
    private String hintMsg = "";
    private String confirmBtnText = "";
    private String cancelBtnText = "";
    private CustomDialog.ClickListenerInterface clickListenerInterface;
    private EditText inputMsg;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    private String content;

    public interface ClickListenerInterface {
        void doConfirm();
        void doCancel();
    }

    public TextFieldDialog(Context context, String title, String hintMsg) {
        super(context, R.style.customDialog);
        this.context = context;
        this.title = title;
        this.hintMsg = hintMsg;
    }

    public TextFieldDialog(Context context, String title, String hintMsg, String confirmBtnText, String cancelBtnText) {
        this(context, title, hintMsg);
        this.confirmBtnText = confirmBtnText;
        this.cancelBtnText = cancelBtnText;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        init();
    }
    public String getInputMsg(){
        return inputMsg.getText().toString();
    }


    public void  setInputMsg(String msg){
        inputMsg.setText(msg);
    }
    public void init() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.text_field_dialog_layout, null);
        setContentView(view);
        TextView tvTitle = view.findViewById(R.id.my_dialog_title);
        inputMsg = view.findViewById(R.id.my_dialog_text_filed);
        inputMsg.setHint(hintMsg);
        Button btnConfirm = view.findViewById(R.id.btn_main_activity_comfirm);
        Button btnCancel = view.findViewById(R.id.btn_main_activity_cancel);

        tvTitle.setText(title);

        if (!confirmBtnText.isEmpty() && !cancelBtnText.isEmpty()) {
            btnConfirm.setText(confirmBtnText);
            btnCancel.setText(cancelBtnText);
        }

        btnConfirm.setOnClickListener(new TextFieldDialog.CustomDialogClickListener());
        btnCancel.setOnClickListener(new TextFieldDialog.CustomDialogClickListener());

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
    }

    public void setClicklistener(CustomDialog.ClickListenerInterface clickListenerInterface) {
        this.clickListenerInterface = clickListenerInterface;
    }

    private class CustomDialogClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int id = v.getId();
            switch (id) {
                case R.id.btn_main_activity_comfirm:
                    clickListenerInterface.doConfirm();
                    break;
                case R.id.btn_main_activity_cancel:
                    clickListenerInterface.doCancel();
                    break;
            }
        }
    }

}
